package cn.alf.ssh.mlxt.action;

import javax.annotation.Resource;

import cn.alf.ssh.mlxt.service.LoginService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 19879L;
	private String username;
	private String password;
	
	@Resource private LoginService loginService;
	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		String result = loginService.login(username, password);
		if("success".equals(result)){
			return "success";
		} 
		if("password".equals(result)){
			this.addFieldError("password", "password error");
			return "input";
		}
		this.addFieldError("username", "name not exist");
		return "fail";
	}
	
	@Override
	public void validate() {
		if(null == this.getUsername() || "".equals(this.getUsername().trim())){
			this.addFieldError("username", "username required");
		}
		if(null == this.getPassword() || "".equals(this.getPassword().trim())){
			this.addFieldError("password", "password required");
		}
	}
}
